package com.wl.controller;

import com.wl.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Title: ProductController
 * @Description: springboot_security_jwt_rsa
 * @Auth: wl
 * @create 2021/11/4
 */
@RestController
@RequestMapping("/product")
public class ProductController {


    @Autowired
    private UserService userService;


    @RequestMapping("/findAll")
    public String findAll(){

        return "这是商品管理";
    }

    /**
     * 对于抛出的异常，全局处理器可接收并处理
     * @return
     */
    @RequestMapping("/testExcep")
    public String testExcep(){
        int i = 5;
        if( i > 4) {
            throw new RuntimeException("发生异常了");
        }
        return "测试异常";
    }

    @RequestMapping("/testNoExcep")
    public String testNoExcep(){
        int i = 5;
        return "测试没有异常";
    }

    /**
     * 对于catch起来的代码，如果发生异常了，全局异常处理器是捕捉不到的
     * @return
     */
    @RequestMapping("/testTryExcep")
    public String testTryExcep(){
        int i = 5;
        try {
            if( i > 4){
                throw new RuntimeException("发生异常了");
            }
        }catch (Exception e){
            System.out.println("测试发生异常了");
        }
        return "测试没有异常";
    }

    /**
     * 测试service异常
     * 对于service也一样，如果代码try了，那么全局异常处理器就捕捉不到异常
     * @return
     */
    @RequestMapping("/testSericeExcep")
    public String testSericeExcep(int i){
        String s = userService.testServiceExcep(i);
        return s;
    }

}
